#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

#import "FWApplication.h"
#import "FWListViewDelegate.h"
#import "FWApplicationDelegate.h"
#import "FWPreferences.h"

#define localizeGlobal(str) [self.plugin.globalBundle localizedStringForKey:str value:str table:nil]


extern NSInteger const kStatusLineIndex;
extern NSInteger const kToolbarIndex;

extern NSString* const kUniqueIdStatusBar;
extern NSString* const kUniqueIdToolBar;
extern NSString* const kUniqueIdMessage;
extern NSString* const kUniqueIdInfo;

typedef enum {
    FWDatasourceTypeNone = -1,
    FWDatasourceTypeAutomatic = 0,
    FWDatasourceTypePro = 1,
    FWDatasourceTypeiPhone = 2,
    FWDatasourceTypeLite = 3
} FWDatasourceType;

typedef enum {
    FWAccessoryActionTypeNone = 0,
    FWAccessoryActionTypeTap = 1,
    FWAccessoryActionTypeHold = 2
} FWAccessoryActionType;


@class LIPlugin;
@class LIAction;
@class LIData;
@class SBApplication;
@class LSStatusBarItem;
@class FWDisplayObject;


@protocol FWPluginOptional <NSObject>

@optional

// Statusbar support
- (NSArray*)statusbarItems;
- (NSString*)statusbarCaptionValue;
- (NSString*)statusbarBadgeValue;
- (NSString*)statusbarCaptionIcon;
- (NSString*)statusbarCaptionIcon2;
- (NSInteger)statusbarIndent;

// Toolbar support
- (NSArray*)toolbarItems;

- (void)repeatingEventNotification:(NSNotification*)notification;
- (void)scheduledEventNotification:(NSDate*)scheduledDate;

// Action support
- (FWAccessoryActionType)accessoryActionType:(FWDisplayObject*)item;
- (void)performAcessoryAction:(FWDisplayObject*)item;

// Swipe support
- (void)performSwipeAction:(UISwipeGestureRecognizerDirection)direction;

@end


@interface FWPlugin : NSObject <LIPluginController, LIDataController, FWPluginOptional> 
{
    id<FWListViewDelegate> fwDatasource;
    FWApplication* fwApplication;
    NSDate* lastUpdate;
    NSDate* lastUnconditionalRefresh;
    FWPreferences* preferences;

    NSString* resourceName;
    NSString* notificationName;
    
    volatile BOOL listLock;
    NSObject* mutexLock;

    BOOL nativeOnly;
    
    NSDate* nextScheduledActivity;
    
    NSInteger toolbarRow;
    NSDate* toolbarOpenedDate;

    BOOL debugMode;
    FWDatasourceType datasourceType;
    
    BOOL infoBarOpen;
    BOOL messageBarOpen;
    BOOL statusBarOpen;
}


@property (retain) LIPlugin* liPlugin;
@property (nonatomic, retain) NSDate* lastUpdate;
@property (nonatomic, retain) NSDate* lastUnconditionalRefresh;
@property (retain) NSArray* currentItems;
@property (retain) FWApplication* fwApplication;
@property (retain) id<FWListViewDelegate> fwDatasource;
@property (nonatomic, retain) FWPreferences* preferences;
@property (retain) NSString* notificationName;
@property (retain) NSTimer* timer;
@property (retain) NSString* resourceName;
@property (readonly) NSDate* nextScheduledActivity;
@property (assign, getter=isDebugMode) BOOL debugMode;
@property (readonly) FWDatasourceType datasourceType;
@property (readonly, getter=isNativeOnly) BOOL nativeOnly;
@property (nonatomic, retain) LSStatusBarItem* statusbarIcon;
@property (assign) BOOL shouldRedisplayOnNextUpdate;

@property (nonatomic, retain) NSString* messageBoxLine1;
@property (nonatomic, retain) NSString* messageBoxLine2;
@property (nonatomic, retain) NSString* messageBoxDetail;


// Initialisation and object management
- (id)initWithApplication:(FWApplication*)app;
- (id)initWithApplication:(FWApplication*)app name:(NSString*)name;
- (void)dealloc;

- (void)updateList;
- (void)repeatingNotification:(NSNotification*)notif;
- (void)updateListForNotification;

- (void)resetMessageBox;
- (void)setMessageBox:(NSString*)line1 line2:(NSString*)line2;
- (void)setMessageBox:(NSString*)line1 line2:(NSString*)line2 detail:(NSString*)detail;

// Items to be displayed support
- (void)postUpdatedItem:(FWDisplayObject*)item;
- (void)postUpdatedList:(NSArray*)newList;

- (void)timerNotification:(NSNotification*)notif;
- (void)viewReadyNotification:(NSNotification*)notif;
- (void)screenUndimmedNotification:(NSNotification*)notif;
- (void)windowVisibleNotification:(NSNotification*)notif;

// Display support
- (void)redisplayListOnNewThread:(id)source;
- (void)triggerRedisplay;
- (void)triggerUnconditionalRefresh;
- (void)scheduleUnconditionalRefresh;

// Statusbar support
- (BOOL)isStatusbarVisible;
- (void)setStatusbarVisible:(BOOL)visible;
- (void)updateStatusbar;
- (BOOL)isStatusbarVisible;

// Toolbar support
- (BOOL)isToolbarVisible;
- (void)setToolbarIndex:(NSInteger)index;
- (void)setToolbarIndex:(NSInteger)index updatingRow:(NSInteger)row;
- (void)hideToolbar;
- (NSInteger)toolbarIndex;

// Preview support
- (CGRect)previewCompatibleRect;
- (UIView*)previewCompatibleView:(UIView*)view;

- (void)previewSelected:(LIAction*)action data:(LIData*)data;

- (void)timerTriggered:(NSTimer*)timer;
- (void)scheduleNextTimerEvent:(NSDate*)date;

@end
